﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using System.Threading;
using System.Drawing;
using System.IO;



namespace Lib_Automation
{
    class Program
    {
        static void Main(string[] args)
        {

            try
            {
                DateTime[] d1 = new DateTime[0];

                if (args[0] == "N")
                {
                    d1 = Next();
                }
                else if (args[0] == "P")
                {
                    d1 = Previous();
                }

                StreamWriter sw = new StreamWriter(args[1]);
               long temp = d1[0].ToBinary();
                for (int i = 0; i < d1.Length; i++)
                {
                    sw.WriteLine((d1[i].ToBinary() - temp) / 10000000.0);
                    Console.WriteLine((d1[i].ToBinary() - temp )/ 10000000.0);
                    temp = d1[i].ToBinary();
                }
                sw.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
           

        }

        static DateTime[] Next()
        {
            Lib_HPReproWF.NHPReproHW().SetFocus();
            (Lib_HPReproWF.NQueueGrid(Lib_HPReproWF.NHPReproHW()).GetCurrentPattern(TablePattern.Pattern) as TablePattern).GetItem(2, 0);
            AutomationElement fcell = (Lib_HPReproWF.NQueueGrid(Lib_HPReproWF.NHPReproHW()).GetCurrentPattern(TablePattern.Pattern) as TablePattern).GetItem(2, 0);
            MouseEvent.MouseMove((int)fcell.Current.BoundingRectangle.X + 15, (int)fcell.Current.BoundingRectangle.Y + 15);
            MouseEvent.MouseClick();
            AutomationElement PreviousPageButton = Lib_HPReproWF.NDefAutomationId(Lib_HPReproWF.NHPReproHW(), TreeScope.Descendants, "PreviousPageButton");
            AutomationElement NextPageButton = Lib_HPReproWF.NDefAutomationId(Lib_HPReproWF.NHPReproHW(), TreeScope.Descendants, "NextPageButton");
            Bitmap[] arr = new Bitmap[0];
            DateTime[] pagetime = new DateTime[0];
            while (true)
            {
                Bitmap temp = CutScreen.Cut(Lib_HPReproWF.NDefAutomationId(Lib_HPReproWF.NHPReproHW(), TreeScope.Descendants, "ImagePreview").Current.BoundingRectangle);
                if (arr.Length > 2)
                {
                    if (!BitMapCheck.Check(arr[arr.Length - 1], temp))
                    {
                        Array.Resize(ref arr, arr.Length + 1);
                        Array.Resize(ref pagetime, pagetime.Length + 1);
                        arr[arr.Length - 1] = temp;
                        pagetime[pagetime.Length - 1] = DateTime.Now;
                    }
                }
                else
                {
                    Array.Resize(ref arr, arr.Length + 1);
                    Array.Resize(ref pagetime, pagetime.Length + 1);
                    arr[arr.Length - 1] = temp;
                    pagetime[pagetime.Length - 1] = DateTime.Now;
                }


                if (NextPageButton.Current.IsEnabled)
                {
                    (NextPageButton.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern).Invoke();
                }
                else
                {
                    break;
                }

            }
            return pagetime;
        }

        static DateTime[] Previous()
        {
            Lib_HPReproWF.NHPReproHW().SetFocus();
            TablePattern tp=(Lib_HPReproWF.NQueueGrid(Lib_HPReproWF.NHPReproHW()).GetCurrentPattern(TablePattern.Pattern) as TablePattern);
            tp.GetItem(tp.Current.RowCount-1, 0);
            AutomationElement fcell = tp.GetItem(tp.Current.RowCount - 1, 0);

            MouseEvent.MouseMove((int)fcell.Current.BoundingRectangle.X + 15, (int)fcell.Current.BoundingRectangle.Y + 15);
            MouseEvent.MouseClick();
            AutomationElement PreviousPageButton = Lib_HPReproWF.NDefAutomationId(Lib_HPReproWF.NHPReproHW(), TreeScope.Descendants, "PreviousPageButton");
            AutomationElement NextPageButton = Lib_HPReproWF.NDefAutomationId(Lib_HPReproWF.NHPReproHW(), TreeScope.Descendants, "NextPageButton");
            Bitmap[] arr = new Bitmap[0];
            DateTime[] pagetime = new DateTime[0];
            while (true)
            {
                Bitmap temp = CutScreen.Cut(Lib_HPReproWF.NDefAutomationId(Lib_HPReproWF.NHPReproHW(), TreeScope.Descendants, "ImagePreview").Current.BoundingRectangle);
                if (arr.Length > 2)
                {
                    if (!BitMapCheck.Check(arr[arr.Length - 1], temp))
                    {
                        Array.Resize(ref arr, arr.Length + 1);
                        Array.Resize(ref pagetime, pagetime.Length + 1);
                        arr[arr.Length - 1] = temp;
                        pagetime[pagetime.Length - 1] = DateTime.Now;
                    }
                }
                else
                {
                    Array.Resize(ref arr, arr.Length + 1);
                    Array.Resize(ref pagetime, pagetime.Length + 1);
                    arr[arr.Length - 1] = temp;
                    pagetime[pagetime.Length - 1] = DateTime.Now;
                }


                if (PreviousPageButton.Current.IsEnabled)
                {
                    (PreviousPageButton.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern).Invoke();
                }
                else
                {
                    break;
                }

            }
            return pagetime;
        }
    }
}
